﻿
//每页数量
var pagesize = 10;

//当前页码
var index = 0;

//分类
var categorytype = 0;

//搜索内容
var title = "";

//初始化类型
InitCategory();

//初始化按钮事件
MenuOperate();

//初始化分页
InitPager();

//初始化分页
function InitPager() {

    pagesize = $("#pagenumber").val();
    categorytype = $("#categorytype").val();
    if (categorytype === "0")
        categorytype = null;
    index = parseInt($("#pagerindex").val());
    title = $("#searchtext").val();

    var html = "";
    var count = $("#pagerhidden").val();
    var pagenumber = parseInt(count / pagesize) + 1;
    if (pagenumber === 1) {
        html = "<li class='pagerstart'  value='1'><a class='pagerstart'  value='1'>«</a></li>" + "<li class='am-active pager' value='1' ><a class='pager' value='1' >1</a></li>" + "<li class='pagerend'  value='1'><a class='pagerend'  value='1'>»</a></li> ";
    } else if (pagenumber > 1) {
        html += "<li class='pagerstart'  value='1'><a class='pagerstart' value='1'>«</a></li>";
        for (var i = 1; i < pagenumber + 1; i++) {
            if (index === i)
                html += "<li class='am-active pager' value=" + i + ">" + "<a class='pager' value=" + i + ">" + i + "</a></li>";
            else
                html += "<li class='pager'  value=" + i + ">" + "<a class='pager' value=" + i + ">" + i + "</a></li>";
        }
        html += "<li class='pagerend' value=" + pagenumber + " ><a class='pagerend'  value=" + pagenumber + ">»</a></li>";
    }
    $(".pager").remove();
    $(".pagerstart").remove();
    $(".pagerend").remove();
    $("#pager").append(html);

    if (count > 0) {
        $.ajax({
            url: "/Easyui/Content/ListContentJson",
            type: "get",
            data: { index: index, pagesize: pagesize, categorytype: categorytype, title: title },
            success: function (result) {
                html = "";
                for (var j = 0; j < result.data.length; j++)
                    html += template("articletpl", result.data[j]);
                $("#pagecount").html(result.count);
                $("#articlelist").html(html);
            }
        });
    }
}

$(document).on("click", "#categorytypeselect .am-selected-text", function () { //分类过滤  
    InitPager();
});

$(document).on("click", "#pager .am-selected-text", function () { //分页数量  
    $("#pagerindex").val(1);
    InitPager();
});

$(document).on("click", ".pager", function () { //页码
    if ($(this).attr("value") !== $("#pagerindex").val()) {
        $("#pagerindex").val($(this).attr("value"));
        InitPager();
    }
});

$(document).on("click", ".pagerstart", function () { //首页
    if ($(this).attr("value") !== $("#pagerindex").val()) {
        $("#pagerindex").val(1);
        InitPager();
    }
});

$(document).on("click", ".pagerend", function () { //末页
    if ($(this).attr("value") !== $("#pagerindex").val()) {
        var count = parseInt(parseInt($("#pagerhidden").val()) / pagesize);
        $("#pagerindex").val(count + 1);
        InitPager();
    }
});

//绑定右键菜单事件
function TabEvent() {
    var jq = top.jQuery;
    //双击关闭TAB选项卡
    jq(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children("span").text();
        jq("#tabs").tabs("close", subtitle);
    });

    //绑定右键菜单选项
    jq(".tabs-inner").bind("contextmenu",
        function (e) {
            jq("#menus").menu("show",
                {
                    left: e.pageX,
                    top: e.pageY
                });
            var subtitle = $(this).children("span").text();
            jq("#menus").data("currtab", subtitle);
            return false;
        });

    //关闭当前
    jq("#menu-tabclose").click(function () {
        var currtab_title = jq("#menus").data("currtab");
        jq("#tabs").tabs("close", currtab_title);
    });

    //全部关闭
    jq("#menu-tabcloseall").click(function () {
        jq(".tabs-closable").each(function (i, n) {
            var t = $(n).text();
            jq("#tabs").tabs("close", t);
        });
    });

    //关闭除当前之外 
    jq("#menu-tabcloseother").click(function () {
        var currtab_title = jq("#menus").data("currtab");
        jq(".tabs-inner span").each(function (i, n) {
            var t = $(n).text();
            if (t != currtab_title)
                jq("#tabs").tabs("close", t);
        });
    });

    //关闭当前右侧 
    jq("#menu-tabcloseright").click(function () {
        var nextall = jq(".tabs-selected").nextAll();
        if (nextall.length === 0) {
            jq.messager.alert("系统提示", "右侧暂无页面", "error");
            return false;
        }
        nextall.each(function (i, n) {
            var t = jq("a:eq(0) span", $(n)).text();
            jq("#tabs").tabs("close", t);
        });
        return false;
    });

    //关闭当前左侧 
    jq("#menu-tabcloseleft").click(function () {
        var prevall = jq(".tabs-selected").prevAll();
        if (prevall.length === 0) {
            jq.messager.alert("系统提示", "左侧暂无页面", "error");
            return false;
        }
        prevall.each(function (i, n) {
            var t = jq("a:eq(0) span", $(n)).text();
            jq("#tabs").tabs("close", t);
        });
        return false;
    });

    //退出
    jq("#menu-exit").click(function () {
        jq("#menus").menu("hide");
    });
}

function MenuOperate() {

    $("#searchbtn").click(function () {  //搜索按钮  
        if ($("#searchtext").val().trim() !== "")
            InitPager();
    });

    $("#addarticle").click(function () {
        var subtitle = "添加文章";
        var url = "/EasyUI/Content/Add";
        var jq = top.jQuery;

        if (!jq("#tabs").tabs("exists", subtitle)) {
            jq("#tabs").tabs("add",
                {
                    title: subtitle,
                    //href: '/EasyUI/DataTable', //无法跨域 
                    content: '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:95%;"></iframe>', //url页面，可跨域
                    closable: true,
                    width: jq("#maincontext").width() - 10,
                    height: jq("#maincontext").height() - 26
                });
        } else {
            jq("#tabs").tabs("select", subtitle);
        }
        TabEvent(); //重新绑定事件 
    });

    //全选，取消
    $("#selectall").click(function () {
        if ($(this).hasClass("active")) {
            $("input[name='articleselect']").each(function () {
                $(this).removeAttr("checked");
                $(this).removeClass("active");
            });
            $(this).removeClass("active");
            $(this).html("全选");
        } else {
            $("input[name='articleselect']").each(function () {
                $(this).prop("checked", true);
                $(this).addClass("active");
            });
            $(this).addClass("active");
            $(this).html("取消全选");
        }
    });

    //批量删除
    $("#deleteall").click(function () {
        var ids = "";
        $("input[name='articleselect']").each(function () {
            if ($(this).hasClass("active"))
                ids += $(this).val() + ",";
        });
        ids = StrTrim(ids, ",");
        if (ids !== "") {
            $.ajax({
                url: "/EasyUI/Content/DeleteAllArticle",
                type: "post",
                data: { "__RequestVerificationToken": $("[name=__RequestVerificationToken]").val(), ids: ids },
                success: function (result) {
                    if (typeof (result) !== "object")
                        result = JSON.parse(result);
                    if (result !== null && typeof (result) === "object") {
                        if (result.code === 200) {
                            OperateMsg("温馨提示", result.msg);
                        } else if (result.code === 401)
                            $.messager.alert("获取文章列表", result.msg, "error");
                        else if (result.code === 402) {
                            ChangeImage();
                            $.messager.alert("异常提醒", result.msg, "error");
                        } else
                            $.messager.alert("异常提醒", "系统异常", "error");
                    } else
                        $.messager.alert("异常提醒", "系统异常", "error");
                },
                error: function () {
                    $.messager.alert("异常错误", "异常错误", "error");
                }
            });
        }
    });

    //选中
    $(document).on("click", ".mainitem", function (event) {
        if ($(this).hasClass("active")) {
            $(this).find("input[name='articleselect']").removeAttr("checked");
            $(this).find("input[name='articleselect']").removeClass("active");
            $(this).children("div").css("border", "");
            $(this).removeClass("active");
            event.stopPropagation();
        } else {
            $(this).find("input[name='articleselect']").prop("checked", true);
            $(this).find("input[name='articleselect']").addClass("active");
            $(this).children("div").css("border", "1px solid red");
            $(this).addClass("active");
            event.stopPropagation();
        }
    });

    //勾选
    $(document).on("click", "input[name='articleselect']", function (event) {
        if ($(this).hasClass("active")) {
            $(this).parents(".mainitem").children("div").css("border", "");
            $(this).removeClass("active");
            event.stopPropagation();
        } else {
            $(this).parents(".mainitem").children("div").css("border", "1px solid red");
            $(this).addClass("active");
            event.stopPropagation();
        }
    });

    //点击图片
    $(document).on("click", ".itemimage", function (event) {
        var id = $(this).parents(".mainitem").find(".article_item").val();
        window.location.href = "/EasyUI/Content/Edit?id=" + id;
        event.stopPropagation(); //防止冒泡相应父级事件
        //return false;  //防止冒泡相应父级事件
    });
}

function InitCategory() {
    $.ajax({
        url: "/EasyUI/Category/GetCategoryList",
        type: "post",
        data: { "__RequestVerificationToken": $("[name=__RequestVerificationToken]").val() },
        success: function (result) {
            if (typeof (result) !== "object")
                result = JSON.parse(result);
            if (result !== null && typeof (result) === "object") {
                if (result.code === 200) {
                    var html = "<option value='0' >全部</option>";
                    for (var i = 0; i < result.msg.length; i++)
                        html += "<option value=" + result.msg[i].CategoryID + ">" + result.msg[i].CategoryName + "</option>";
                    $("#categorytype").html(html);
                } else if (result.code === 401)
                    $.messager.alert("获取文章列表", result.msg, "error");
                else if (result.code === 402) {
                    ChangeImage();
                    $.messager.alert("异常提醒", result.msg, "error");
                } else
                    $.messager.alert("异常提醒", "系统异常", "error");
            } else
                $.messager.alert("异常提醒", "系统异常", "error");
        },
        error: function () {
            $.messager.alert("异常错误", "异常错误", "error");
        }
    });

    //显示隐藏
    $(document).on("click", ".article_display", function (event) {
        var id = $(this).parents(".mainitem").find(".article_item").val();
        if ($(this).parents(".mainitem").find(".article_item").attr("displaystatus") === "0") {
            OperateArticle(1, id, $(this));
            $(this).parents(".mainitem").find(".article_item").attr("displaystatus", "1");
        } else {
            OperateArticle(2, id, $(this));
            $(this).parents(".mainitem").find(".article_item").attr("displaystatus", "0");
        }
        event.stopPropagation();
    });

    //编辑
    $(document).on("click", ".article_edit", function (event) {
        var id = $(this).parents(".mainitem").find(".article_item").val();
        var subtitle = $(this).parents(".mainitem").find(".tpl-i-title").html();
        var url = "/EasyUI/Content/Edit?id=" + id;
        var jq = top.jQuery;

        if (!jq("#tabs").tabs("exists", subtitle)) {
            jq("#tabs").tabs("add",
                {
                    title: subtitle,
                    //href: '/EasyUI/DataTable', //无法跨域 
                    content: '<iframe name="mainFrame" scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:95%;"></iframe>', //url页面，可跨域
                    closable: true,
                    width: jq("#maincontext").width() - 10,
                    height: jq("#maincontext").height() - 26
                });
        } else {
            jq("#tabs").tabs("select", subtitle);
        }
        TabEvent(); //重新绑定事件 
        event.stopPropagation();
    });

    //置顶
    $(document).on("click", ".article_top", function (event) {
        var id = $(this).parents(".mainitem").find(".article_item").val();
        OperateArticle(3, id, $(this));
        event.stopPropagation();
    });

    //删除
    $(document).on("click", ".article_delete", function (event) {
        var id = $(this).parents(".mainitem").find(".article_item").val();
        OperateArticle(4, id, $(this));
        event.stopPropagation();
    });
}

function OperateArticle(flag, id, thisdom) {
    $.ajax({
        url: "/EasyUI/Content/OperateArticle",
        type: "post",
        data: { "__RequestVerificationToken": $("[name=__RequestVerificationToken]").val(), ArticleID: id, flag: flag },
        success: function (result) {
            if (typeof (result) !== "object")
                result = JSON.parse(result);
            if (result !== null && typeof (result) === "object") {
                if (result.code === 200) {
                    switch (flag) {
                        case 1: //显示
                            thisdom.html("显示");
                            break;
                        case 2: //隐藏
                            thisdom.html("隐藏");
                            break;
                        case 3: //置顶 
                            break;
                        case 4: //删除 
                            break;
                    }
                    OperateMsg("温馨提示", "操作成功");
                } else if (result.code === 401)
                    $.messager.alert("获取文章列表", result.msg, "error");
                else if (result.code === 402) {
                    ChangeImage();
                    $.messager.alert("异常提醒", result.msg, "error");
                } else
                    $.messager.alert("异常提醒", "系统异常", "error");
            } else
                $.messager.alert("异常提醒", "系统异常", "error");
        },
        error: function () {
            $.messager.alert("异常错误", "异常错误", "error");
        }
    });
}